#!/usr/bin/env python
import re
import os.path
from languages import *
import getpass

class Config:
	def __init__(self):
        	self.conffile = 'general.conf'
	        self.conf_reg = re.compile(r'^([\bconfig=])')
        
	def check_language(self):
	        if self.check_file(self.conffile):
        		return self.read_file(self.conffile,self.conf_reg)

	def check_file(self):
	        if os.path.exists(self.conffile):
	        	return True
	        else: return False

	#Receive 2 parameters
	#Type: interface, user
	#String: Value to be searched
	#Searches type=string
	def read_file(self,ctype,value):
	        cfile = open(self.conffile)
		lines = cfile.readlines()
        	for i in lines:
	            if re.match((ctype+'='+value),i.strip()):
	            	#check = (i.strip()).replace('config=', '')
	                #if check == 'yes':  return True
	                #else: return False
			return True
		    else: pass
		return False

	def check_user(self,user):
		if self.check_file() == True:
			if self.read_file('user',user) == True:
				return True
			
	#Check Language
	#Check if the system is configured
	def check_config(self,cfile,string):
	        cfile = open(cfile)
	        lines = file.readlines()
	        for i in lines:
	            if string.match(i.strip()):
	                check = (i.strip()).replace('config=', '')
	                if check == 'yes':  return True
	                else: return False
        
	def config_language(self):
	        pass

	def config_type(self):
	        pass
    
	def config_client(self):
	        pass

	def config_user(self):
	# Configure users/passwords allowed to login.
		var = 0
		users = []
		error = 0
		conf = 1
		user_var = 0
		while var == 0:
			while user_var == 0:
				user = []
				user.append(raw_input('\n'+lang_config_server_user+'\n'))
				if self.check_user(user[0]) == True: 
					print '\n'+lang_config_server_user_exists
					user_var = 0
				else: user_var = 1
			user_var = 0
			while error == 0:
				pass1 = getpass.getpass('\n'+lang_config_server_user_pass+'\n')
				pass2 = getpass.getpass('\n'+lang_config_server_user_pass1+'\n')
				if pass1 == pass2: error = 1
				else: 
					print '\n',lang_error_unmatch_pass,'\n'
					error = 0
			user.append(pass1)
			users.append(user)
			while conf == 1:
				end = raw_input('\n'+lang_config_server_user_other+'\n')
				if end == 'y':	
					var = 0
					conf = 0
				elif end == 'n':	
					var = 1
					conf = 0
				else: 
					print '\n'+lang_config_server_user_yn+'\n'
					conf = 1
			error = 0
			conf = 1
		return users

	def config_server(self):
		interface = raw_input('\n'+lang_config_server_interface+'\n')
		users = self.config_user()
		print interface,'\n',users,'\n'
		#Verify if user already exists
		
		pass


        
